package me.jessyan.armscomponent.app.mvp.ui.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.widget.toast.ToastView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.app.UserConstants;
import me.jessyan.armscomponent.app.di.component.DaggerUserSetPayPasswordComponent;
import me.jessyan.armscomponent.app.mvp.contract.user.UserSetPayPasswordContract;
import me.jessyan.armscomponent.app.mvp.presenter.user.UserSetPayPasswordPresenter;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/19/2021 23:17
 * Description: 设置支付密码
 */
@Route(path = RouterHub.USER_SETPAYPASSWORDACTIVITY)
public class UserSetPayPasswordActivity extends BaseActivity<UserSetPayPasswordPresenter> implements UserSetPayPasswordContract.View {

    String username;
    String password;

    String smsCode;
    String inviteCode;
    String countryCode;

    @BindView(R.id.user_et_pay_password)
    EditText user_et_pay_password;

    @BindView(R.id.user_et_pay_password_comfirm)
    EditText user_et_pay_password_comfirm;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserSetPayPasswordComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.user_activity_set_pay_password; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        loadParams();
        loadTitle();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastView.getInstance().showToast(this,message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnClick({R.id.user_btn_login2})
    public void onClicked(View v){
        int id = v.getId();
        if(id == R.id.user_btn_login2){
            if (Utils.isFastClick()) {
                if(comfirmPassword()){
//                    Utils.navigation(this, RouterHub.USER_SETUSERINFOACTIVITY);
                    ARouter.getInstance().build(RouterHub.USER_SETUSERINFOACTIVITY)
                            .withString(UserConstants.USER_NAME, username)
                            .withString(UserConstants.PASSWORD, password)
                            .withString(UserConstants.SMS_CODE, smsCode)
                            .withString(UserConstants.INVITE_CODE, inviteCode)
                            .withString(UserConstants.PAY_PASSWORD, getPayPassword())
                            .navigation();
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                }else{
                    showMessage("两次支付密码输入不一致");
                }
            }
        }
    }

//    public void onLoginClicked(View v) {
//        if(Utils.isFastClick()){
//            Utils.navigation(this, RouterHub.USER_SETUSERINFOACTIVITY);
//            finish();
//            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//        }
//    }

    private void loadParams(){
        username = getIntent().getStringExtra(UserConstants.USER_NAME);
        password = getIntent().getStringExtra(UserConstants.PASSWORD);
        smsCode = getIntent().getStringExtra(UserConstants.SMS_CODE);
        inviteCode = getIntent().getStringExtra(UserConstants.INVITE_CODE);
        countryCode = getIntent().getStringExtra(UserConstants.COUNTRY_CODE);
    }

    private void loadTitle() {
        String title = "设置支付密码";
        setTitle(title);
    }

    @Override
    public String getPayPassword() {
        return user_et_pay_password.getText().toString().trim();
    }

    @Override
    public String getPayPasswordComfirm() {
        return user_et_pay_password_comfirm.getText().toString().trim();
    }

    @Override
    public boolean comfirmPassword() {
        return getPayPassword().equals(getPayPasswordComfirm());
    }
}
